package com.nowcoder.community.controller;

import com.nowcoder.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.time.LocalDate;

/**
 * UV, DAU 统计
 * */
@Controller
@RequestMapping("/data")
public class DataController {

    private final String dateFormat = "yyyy-MM-dd";

    @Autowired
    private DataService dataService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public String getDataPage(){
        return "/site/admin/data";
    }

    @PostMapping("/uv")
    public String uvStat(
            @DateTimeFormat(pattern = dateFormat) LocalDate start,
            @DateTimeFormat(pattern = dateFormat) LocalDate end,
            Model model){
        long uv = dataService.getUV(start, end);
        model.addAttribute("uvStart", start);
        model.addAttribute("uvEnd", end);
        model.addAttribute("uv", uv);
        return "forward:/data";
    }

    @PostMapping("/dau")
    public String dauStat(
            @DateTimeFormat(pattern = dateFormat) LocalDate start,
            @DateTimeFormat(pattern = dateFormat) LocalDate end,
            Model model
    ){
        long dau = dataService.getDAU(start, end);
        model.addAttribute("dauStart", start);
        model.addAttribute("dauEnd", end);
        model.addAttribute("dau", dau);
        return "forward:/data";
    }
}
